package com.woniu1.cglibproxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

//cglib动态代理,支持没有接口的类
//需要jar包。来实现动态代理
public class CglibProxyFactory {
    public Object creat(Object obj){
        Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("前置方法");
                Object o1 = method.invoke(o, objects);
                System.out.println("后置方法");
                return o1;
            }
        });
        return obj;
    }
}
